package com.citywy.dao;

import com.citywy.domain.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

//开启和关闭二级缓存
//@CacheNamespace(blocking = true)
public interface UserDao {

    /**
     * 查询所有
     * @return
     */
    @Select("select * from user")
    @Results(//代替的是标签<resultMap>
            id="userMap",
            value={
                    //@Result代替<id>和<result>标签
                    @Result(id=true,column = "id",property = "userId"),
                    @Result(column = "username",property = "userName"),
                    @Result(column = "sex",property = "userSex"),
                    @Result(column = "birthday",property = "userBirthday"),
                    @Result(column = "address",property = "userAddress")
            })
    List<User> findAll();

    /**
     * 根据ID查询用户
     */
    @Select("select * from user where id=#{uid}")
    @ResultMap("userMap")
    User findById(Integer userId);

    /**
     * 保存操作
     */
    @Insert("insert into user(username,sex,birthday,address) values (#{userName},#{userSex},#{userBirthday},#{userAddress})")
    @SelectKey(keyColumn = "id",keyProperty = "userId",resultType = Integer.class,before = false,statement = {"select last_insert_id()"})
    int saveUser(User user);

    /**
     * 更新操作
     */
    @Update("update user set username=#{userName},address=#{userAddress},sex=#{userSex},birthday=#{userBirthday} where id=#{userId}")
    int updateUser(User user);

    /**
     * 删除用户
     */
    @Delete("delete from user where id=#{uid}")
    int deleteUser(Integer userId);

    /**
     * 查询使用聚合函数
     */
    @Select("select count(*) from user")
    int findTotal();

    /**
     * 模糊查询
     */
    @Select("select * from user where username like #{username}")
    @ResultMap("userMap")
    List<User> findByName(String name);
}
